package com.android.tiango.baba.view;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.tiango.baba.util.DensityUtil;

public class Paging implements OnClickListener {

	public Gallery mGallery;
	TextSwitcher mTextSwitcher;
	/**
	 * 分页选择的页数 以0开头
	 */
	private int mPaging = 0;
	private Context mContext;
	private View mView;
	private int mCount;

	private int ScreenW;
	private int ScreenH;
	private LayoutInflater mInflater;
	private PopupWindow mPopupWindow;
	private viewholder mViewholder;
	private ImageView mSure, mDismiss;
	private TextView mSelect;
	private WindowManager.LayoutParams mLayout;
	private galleryadapter mGalleryAdapter;
	private PageChoseListener pageListener;

	public Paging(Context context, int count) {
		mContext = context;
		mCount = count;
		ScreenW = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth();
		ScreenH = ((Activity) mContext).getWindowManager().getDefaultDisplay().getHeight();
	}

	/**
	 * 弹出分页
	 * 
	 * @param ScreenW
	 *        代表屏幕宽度
	 */
	public void showPaging(View v) {

		getPopuptwindow(v);
	}

	/**
	 * 显示和关闭 mPopupWindow
	 * 
	 * @param v
	 */
	private void getPopuptwindow(View v) {
		if (null != mPopupWindow) {
			if (mPopupWindow.isShowing()) {
				mPopupWindow.dismiss();
			} else {
				mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
				mLayout = ((Activity) mContext).getWindow().getAttributes();
				mLayout.alpha = 0.6f;
				((Activity) mContext).getWindow().setAttributes(mLayout);
			}
			return;
		} else {
			loadView();
			initPopuptWindow();
			mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);

		}
	}

	/**
	 * 
	 * @return 页数
	 */
	private int getmPaging() {
		return mPaging;
	}

	/**
		
      */

	private void initPopuptWindow() {
		// View popupWindow = layoutInflater.inflate(R.layout.gallery, null);
		// popupWindow.setBackgroundColor(Color.BLACK);

		// 创建一个PopupWindow
		// 参数1：contentView 指定PopupWindow的内容
		// 参数2：width 指定PopupWindow的width
		// 参数3：height 指定PopupWindow的height
		mPopupWindow = new PopupWindow(mView, ScreenW, DensityUtil.dip2px(mContext, 150));
		mPopupWindow.setFocusable(true);
		mPopupWindow
				.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.bg_image));
		mPopupWindow.setOutsideTouchable(true);

		mLayout = ((Activity) mContext).getWindow().getAttributes();
		mLayout.alpha = 0.6f;
		((Activity) mContext).getWindow().setAttributes(mLayout);
		mPopupWindow.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss() {
				// TODO Auto-generated method stub
				mLayout = ((Activity) mContext).getWindow().getAttributes();
				mLayout.alpha = 1.0f;
				((Activity) mContext).getWindow().setAttributes(mLayout);
			}
		});

	}

	private void loadView() {
		// TODO Auto-generated method stub
		mInflater = LayoutInflater.from(mContext);
		mView = mInflater.inflate(R.layout.layout_grallery, null);

		mGallery = (Gallery) mView.findViewById(R.id.gallery1);

		mSelect = (TextView) mView.findViewById(R.id.tv_select);
		mDismiss = (ImageView) mView.findViewById(R.id.bt_dismiss);
		mSure = (ImageView) mView.findViewById(R.id.bt_sure);

		mDismiss.setOnClickListener(this);
		mSure.setOnClickListener(this);

		mViewholder = new viewholder();
		mGalleryAdapter = new galleryadapter();
		mGallery.setAdapter(mGalleryAdapter);
		mGallery.setSelection(0);
		mSelect.setText("1/" + mCount);

		mGallery.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				// position2=position;

				mSelect.setText((position + 1) + "/" + mCount);

				mPaging = position;
				mGalleryAdapter.notifyDataSetChanged();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				mViewholder.tv.setBackgroundDrawable(null);
			}
		});

	}

	private class galleryadapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mCount;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub

			View view = mInflater.inflate(R.layout.item_gallery, null);
			mViewholder.tv = (TextView) view.findViewById(R.id.tv_gallery);
			mViewholder.tv.setText("" + (position + 1));
			if (mPaging == position) {
				mViewholder.tv.setBackgroundResource(R.drawable.fengexian);
				mViewholder.tv.getPaint().setFakeBoldText(true);
				mViewholder.tv.setTextSize(30);
			} else {
				mViewholder.tv.setBackgroundDrawable(null);
				mViewholder.tv.getPaint().setFakeBoldText(false);
				mViewholder.tv.setTextSize(24);
			}
			return view;
		}

	}

	private class viewholder {
		private TextView tv;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bt_sure:
			mPopupWindow.dismiss();
			mPopupWindow = null;
			if (null!=pageListener) {
				pageListener.pageChose(getmPaging());
			}
			break;
		case R.id.bt_dismiss:
			mPopupWindow.dismiss();
			break;
		default:
			break;
		}
	}

	public interface PageChoseListener {
		public void pageChose(int page);
	}

	public void setPageChoseListener(PageChoseListener pageListener) {
		this.pageListener = pageListener;
	}
}
